package com.parking.status.sync.config;

import com.parking.status.sync.netty.server.ChannelRepository;
import com.parking.status.sync.netty.server.RequestFinalDecoder;
import com.parking.status.sync.netty.server.ServerChannelInitializer;
import com.parking.status.sync.netty.server.TCPServer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by Administrator on 2015/9/17.
 */
@Configuration
public class NettyTcpConfig {

//    @Value("${tcp.ip:127.0.0.1}")
//    private String tcpIp;
//
//    @Value("${tcp.port:20000}")
//    private int tcpPort;
//
//    @Value("${boss.thread.count:2}")
//    private int bossCount;
//
//    @Value("${worker.thread.count:2}")
//    private int workerCount;
//
//    @Value("${so.keepalive:true}")
//    private boolean keepAlive;
//
//    @Value("${so.backlog:100}")
//    private int backlog;
//
//    @Autowired
//    ServerChannelInitializer serverChannelInitializer;
//
//    @SuppressWarnings("unchecked")
//    @Bean(name = "serverBootstrap")
//    public ServerBootstrap bootstrap() {
//        ServerBootstrap serverBootstrap = new ServerBootstrap();
//        serverBootstrap.group(bossGroup(), workerGroup())
//                .channel(NioServerSocketChannel.class)
//                .handler(new LoggingHandler(LogLevel.DEBUG))
//                .childHandler(serverChannelInitializer);
//        Map<ChannelOption<?>, Object> tcpChannelOptions = tcpChannelOptions();
//        Set<ChannelOption<?>> keySet = tcpChannelOptions.keySet();
//        for (@SuppressWarnings("rawtypes") ChannelOption option : keySet) {
//            serverBootstrap.option(option, tcpChannelOptions.get(option));
//        }
//        return serverBootstrap;
//    }
//
//    //@Bean(name = "bossGroup", destroyMethod = "shutdownGracefully")
//    public NioEventLoopGroup bossGroup() {
//        return new NioEventLoopGroup(bossCount);
//    }
//
//    //@Bean(name = "workerGroup", destroyMethod = "shutdownGracefully")
//    public NioEventLoopGroup workerGroup() {
//        return new NioEventLoopGroup(workerCount);
//    }
//
//    @Bean(name = "tcpChannelOptions")
//    public Map<ChannelOption<?>, Object> tcpChannelOptions() {
//        Map<ChannelOption<?>, Object> options = new HashMap<ChannelOption<?>, Object>();
//        options.put(ChannelOption.SO_KEEPALIVE, keepAlive);
//        options.put(ChannelOption.SO_BACKLOG, backlog);
//        return options;
//    }
//
//    @Bean(name = "channelRepository")
//    public ChannelRepository channelRepository() {
//        return new ChannelRepository();
//    }
//
//    @Bean(name = "tcpServerSocketAddress")
//    public InetSocketAddress tcpPort() {
//        return new InetSocketAddress(tcpPort);
//    }

//
    @Bean
    public RequestFinalDecoder requestFinalDecoder() {
        return new RequestFinalDecoder();
    }

    @Bean(name = "serverChannelInitializer")
    public ServerChannelInitializer serverChannelInitializer() {
        return new ServerChannelInitializer();
    }
}
